import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import Comments from '@/view/comments'
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/view/login/loginIndex.vue"),
  },
  {
    path: "/",
    component: () => import("@/view/layout/layoutIndex.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/view/home/index.vue"),
      },
      {
        path: "/comments",
        name: "comments",
        component: Comments,
      },
      {
        path: "/article",
        name: "article",
        component: () => import("@/view/article/index.vue"),
      },
      {
        path: '/flowanalysis',
        name: 'flowanalysis',
        component: () => import("@/view/flowanalysis/index.vue"),
      },
      {
        path: '/fans',
        name: 'fans',
        component: () => import("@/view/fans/index.vue"),
      },
      {
        path: "/publish",
        name: "publish",
        component: () => import("@/view/publish/index.vue"),
      },
      {
        path: "/usersettings",
        name: "usersettings",
        component: () => import("@/view/usersettings/index.vue"),
      },
    ],
  },
];
// route 路由信息
// router 路由实例化对象
const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  // 如果要访问的页面不是 /login，校验登录状态
  // 如果没有登录，则跳转到登录页面
  // 如果登录了，则允许通过
  // 允许通过
  // next()

  const user = JSON.parse(window.localStorage.getItem('user'))

  // 校验非登录页面的登录状态
  if (to.path !== '/login') {
    if (user) {
      // 已登录，允许通过
      next()
    } else {
      // 没有登录，跳转到登录页面
      next('/login')
    }
  } else {
    // 登录页面，正常允许通过
    next()
  }
})


export default router;
